package com.elective.courseselect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elective.courseselect.dao.QuestionDao;
import com.elective.courseselect.dao.QuestionnaireDao;
import com.elective.courseselect.entity.QuestionEntity;
import com.elective.courseselect.entity.QuestionnaireEntity;
import com.elective.courseselect.service.QuestionnaireService;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;



@Service("questionnaireService")
@Transactional(rollbackFor = Exception.class)
public class QuestionnaireServiceImpl extends ServiceImpl<QuestionnaireDao, QuestionnaireEntity> implements QuestionnaireService {

    @Autowired
    QuestionnaireDao questionnaireDao;

    @Autowired
    QuestionDao questionDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<QuestionnaireEntity> page = this.page(
                new Query<QuestionnaireEntity>().getPage(params),
                new QueryWrapper<QuestionnaireEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<QuestionnaireEntity> searchQuestionnaireList(Integer type) {
        QueryWrapper<QuestionnaireEntity> questionnaireEntityQueryWrapper = new QueryWrapper<>();
        questionnaireEntityQueryWrapper.eq("type", type);
        List<QuestionnaireEntity> list = this.list(questionnaireEntityQueryWrapper);
        return list;
    }

    @Override
    public PageUtils searchCharacterQuestionList(Map<String, Object> params) {
        QueryWrapper<QuestionnaireEntity> questionnaireEntityQueryWrapper = new QueryWrapper<>();
        questionnaireEntityQueryWrapper.eq("type", 0);
        IPage<QuestionnaireEntity> page = this.page(
                new Query<QuestionnaireEntity>().getPage(params),
                questionnaireEntityQueryWrapper
        );

        for (QuestionnaireEntity record : page.getRecords()) {
            switch (record.getType()) {
                case PERSONALITY_QUESTIONNAIRE:
                    record.setTypeName("性格调查问卷");
                    break;
                case COURSE_WILL_QUESTIONNAIRE:
                    record.setTypeName("课程意愿调查问卷");
                    break;
                default:
                    break;
            }


            switch (record.getStatus()) {
                case SUBMIT_AN_APPLICATION:
                    record.setStatusName("申请中");
                    break;
                case APPLICATION_PASS:
                    record.setStatusName("正常");
                    break;
                case ABANDON:
                    record.setStatusName("弃用");
                    break;
                default:
                    break;
            }

        }

        return new PageUtils(page);

    }

    @Override
    public boolean deleteQuestionnaireAndQuestion(List<Integer> asList) {
        for (Integer delQuestionnaireId : asList) {
            //删除系统问卷
            questionnaireDao.deleteById(delQuestionnaireId);
            QuestionEntity questionEntity = new QuestionEntity();
            questionEntity.setQuestionnaireId(delQuestionnaireId);
            QueryWrapper<QuestionEntity> wrapper = new QueryWrapper<>(questionEntity);
            //删除关联题目
            questionDao.delete(wrapper);
        }
        return true;
    }


}